package cn.wolfcode.luowowo.web.controller;

import cn.wolfcode.luowowo.annotion.RequireLogin;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelsService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController {
    //配置属性是在website-api项目中,使用的位置 userInfoServiceImpl 类中
    //如果直接将3个属性添加到userInfoServiceImpl类中,mgrsite项目启动时会报错
    //原因:mgrsite没有这3个配置
    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.msg}")
    private String smsMsg;
    @Value("${sms.appkey}")
    private String smsAppkey;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelsService travelsService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;
    @RequireLogin
    @GetMapping("getCurrentUser")
    public Object getCurrentUser(HttpServletRequest request){
        System.out.println("demo");
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(userInfo);
    }
    @GetMapping("get")
    public Object list(String id){
        return userInfoService.get(id);
    }
    //验证手机号是否已经注册
    @GetMapping("checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);

        return !ret;
    }
    //给手机号发送验证码
    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone){
        Integer count = userInfoService.getCount() +0;
        if (count <= 5){
            userInfoService.sendVerifyCode(phone,smsUrl,smsMsg,smsAppkey);
            return JsonResult.success();
        }else {
            return JsonResult.ParametersError();
        }
        //先接收传过来的手机号
        //再自己生成一个验证码
        //再发送这个验证码到传过来的手机号
        //再对验证码进行缓存(因为后面注册时需要对验证码进行校验)
        //存在redis中,并设置时效性(比如3分钟)
        //最后返回结果提示(成功或失败)
    }
    //注册
    @PostMapping("regist")
    public Object regist(String nickname,String phone,String password,String verifyCode,String rpassword){
        userInfoService.regist(nickname,phone,password,verifyCode,rpassword);
        return JsonResult.success();
    }
    @GetMapping("checkName")
    public Map<String,Boolean> checkName(String _id, String nickname){
        //如果id等于空,则对传过来的name进行验证判断
        HashMap<String, Boolean> map = new HashMap<>();
        UserInfo userInfo = userInfoService.selectByName(nickname);
        if (_id == null) {
            map.put("valid", userInfo == null);
            return map;
        }else {//如果id不为空
            //则判断当前name是否与原name相同,相同则验证通过
            if (nickname.equals(userInfoService.get(_id).getNickname())) {
                map.put("valid", true);
                return map;
            }else {//不同则需要再次验证传入的name是否与其他用户的name相同
                //当employee为空时,value为true,验证通过,
                //当employee不为空时,value为false,验证不通过
                map.put("valid", userInfo == null);
                return map;
            }
        }
    }
    @PostMapping("login")
    public Object login(String username,String password){
        //登录
        UserInfo user = userInfoService.login(username,password);

        //将用户缓存到redis中
        String token = userInfoRedisService.setToken(user);

        //将token和user传过去
        Map<Object, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);

        return JsonResult.success(map);
    }
    /*@GetMapping("strategies/favor")
    public Object strategiesFavor(String sid,String userId){
        return JsonResult.success(userInfoService.get(userId));
    }*/
    @GetMapping("/getLoginUser")
    public Object getTravelIdUser(HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        //通过token获取当前登录用户,以此来判断是否登录
        UserInfo user = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(user);
    }
    @GetMapping("strategies/favor")//用户攻略收藏集合
    public Object strategiesFavor(String sid,String userId){
        List<String> list = strategyStatisVORedisService.getFavorStrategyIds(userId);
        return JsonResult.success(list);
    }
    @GetMapping("travels/favor")//用户攻略收藏集合
    public Object travelsFavor(String tid,String userId){
        List<String> list = travelStatisVORedisService.getFavorTravelIds(userId);
        return JsonResult.success(list);
    }
}
